#include"include/hash.h"



// 哈希函数
int hash(int key,int hash_size){

    return key%hash_size;
}

//初始化LRU缓存
int LRU_cache_init(LRU_cache_t *lru_cache,int capacity,int hash_len){
    // 初始化哈希表
    lru_cache->hash_map=(hash_map_t**)calloc(hash_len,sizeof(hash_map_t*));
   
    //初始化哈希表最大长度
    lru_cache->hash_len=hash_len;
    //初始化双向链表
    lru_cache->linklist=(linklist_t*)calloc(1,sizeof(linklist_t));
    //初始化双向链表最大长度
    lru_cache->linklist->capacity=capacity;
    //初始化双向链表当前长度
    lru_cache->linklist->cursize=0;
    //初始化双向链表头结点
    lru_cache->linklist->header=NULL;
    //初始化双向链表尾结点
    lru_cache->linklist->tail=NULL; 

    // 初始化锁
    pthread_mutex_init(&lru_cache->lock,NULL);
     return 0;
}
// 根据双向链表中的key值，找到对应的哈希表的下标值
node_t *find_node(LRU_cache_t *lru_cache,int key){
    
    pthread_mutex_lock(&lru_cache->lock);
    int hash_index=hash(key,lru_cache->hash_len);
    hash_map_t *hash_map=lru_cache->hash_map[hash_index];
    while(hash_map!=NULL){
        if(hash_map->key==key){
            return hash_map->index;
        }
        hash_map=hash_map->next;
    }
    pthread_mutex_unlock(&lru_cache->lock);
    return NULL;    
}


int LRU_cache_insert(LRU_cache_t *lru_cache, int key, time_t value) {
    pthread_mutex_lock(&lru_cache->lock);

    int hash_index = hash(key, lru_cache->hash_len);
    hash_map_t *cur_map = lru_cache->hash_map[hash_index];
    hash_map_t *prev_map = NULL;

    // 查找哈希表中是否已存在
    while (cur_map) {
        if (cur_map->key == key) break;
        prev_map = cur_map;
        cur_map = cur_map->next;
    }

    if (cur_map) {
        // 已存在，移动到头部并更新 value
        node_t *cur = cur_map->index;
        cur->value = value;

        // 如果不是头部，摘除并插到头部
        if (cur != lru_cache->linklist->header) {
            // 摘除
            if (cur->pre) cur->pre->next = cur->next;
            if (cur->next) cur->next->pre = cur->pre;
            if (lru_cache->linklist->tail == cur) {
                lru_cache->linklist->tail = cur->pre;
            }
            // 插到头部
            cur->pre = NULL;
            cur->next = lru_cache->linklist->header;
            if (lru_cache->linklist->header) {
                lru_cache->linklist->header->pre = cur;
            }
            lru_cache->linklist->header = cur;
        }
        pthread_mutex_unlock(&lru_cache->lock);
        return 0;
    }

    // 不存在，新建节点
    node_t *new_node = (node_t*)calloc(1, sizeof(node_t));
    new_node->key = key;
    new_node->value = value;
    new_node->pre = NULL;
    new_node->next = lru_cache->linklist->header;
    if (lru_cache->linklist->header) {
        lru_cache->linklist->header->pre = new_node;
    }
    lru_cache->linklist->header = new_node;
    if (lru_cache->linklist->tail == NULL) {
        lru_cache->linklist->tail = new_node;
    }
    lru_cache->linklist->cursize++;

    // 插入哈希表
    hash_map_t *new_map = (hash_map_t*)calloc(1, sizeof(hash_map_t));
    new_map->key = key;
    new_map->index = new_node;
    new_map->next = lru_cache->hash_map[hash_index];
    lru_cache->hash_map[hash_index] = new_map;

    pthread_mutex_unlock(&lru_cache->lock);
    return 0;
}


// 删除LRU缓存
int LRU_cache_delete(LRU_cache_t *lru_cache, int key) {
    pthread_mutex_lock(&lru_cache->lock);

    int hash_index = hash(key, lru_cache->hash_len);
    hash_map_t *cur_map = lru_cache->hash_map[hash_index];
    hash_map_t *prev_map = NULL;

    // 查找哈希表节点
    while (cur_map) {
        if (cur_map->key == key) break;
        prev_map = cur_map;
        cur_map = cur_map->next;
    }
    if (!cur_map) {
        pthread_mutex_unlock(&lru_cache->lock);
        return -1; // 不存在
    }

    node_t *cur = cur_map->index;

    // 从链表摘除
    if (cur->pre) cur->pre->next = cur->next;
    else lru_cache->linklist->header = cur->next;
    if (cur->next) cur->next->pre = cur->pre;
    else lru_cache->linklist->tail = cur->pre;
    lru_cache->linklist->cursize--;

    free(cur);

    // 从哈希表摘除
    if (prev_map) prev_map->next = cur_map->next;
    else lru_cache->hash_map[hash_index] = cur_map->next;
    free(cur_map);

    pthread_mutex_unlock(&lru_cache->lock);
    return 0;
}


int LRU_cache_delete_timeout(LRU_cache_t *lru_cache,time_t time,time_t timeout){

     pthread_mutex_lock(&lru_cache->lock);
     //从尾部开始删除，直到找到一个不超时的结点
     node_t *tail=lru_cache->linklist->tail;
     while(tail!=NULL){
        if(time-tail->value<timeout){
            //没有超时
             pthread_mutex_unlock(&lru_cache->lock);
             break;
        }
        //超时了，删除
        LRU_cache_delete(lru_cache,tail->key);
        tail=tail->pre;
     }
     pthread_mutex_unlock(&lru_cache->lock);
      return 0;
}

// 辅助函数：打印缓存内容
void print_cache(LRU_cache_t *lru_cache) {
    pthread_mutex_lock(&lru_cache->lock);
    printf("Cache contents (size: %d):\n", lru_cache->linklist->cursize);
    node_t *current = lru_cache->linklist->header;
    while (current != NULL) {
        printf("  Key: %d, Timestamp: %ld\n", current->key, current->value);
        current = current->next;
    }
    pthread_mutex_unlock(&lru_cache->lock);
    printf("\n");
}

// 辅助函数：获取当前时间戳（秒级）
time_t get_current_timestamp() {
    struct timeval tv;
    gettimeofday(&tv, NULL);
    return tv.tv_sec;
}
